// pages/recipe-edit/composition/index.js
import {
  chemicalAlias
} from '../../../dataSource'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    dataSource: {
      type: Array,
      value: [],
      observer(data) {
        const arr = data.filter(it=> it.elementType !== 'correctionAmount' ).map(it => {
            return {
              name: chemicalAlias[it['elementType']],
              value: isNaN(Number(it['recipeIngredient0'])) ? '0.00' : Number(it['recipeIngredient0']).toFixed(2)
            }
        })
        this.setData({
          dataSourceUse: arr,
          total: arr.reduce((prevalue, currentValue) => {
            return prevalue + Number(currentValue.value)
          }, 0).toFixed(2)
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataSourceUse: [],
    total: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.triggerEvent('close')
    }
  }
})